<?php

require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");
require_once("lib/exception/parameters_exception.php");

require_once("lib/model/action.php");
require_once("model/autorfacade/util/util_autor.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");

class Change_password_action implements Action {

	private $login;
	private $old_password;
	private $new_password;
	
	public function __construct($login, $old_password, $new_password) {
		
		$this->login = $login;
		$this->old_password = $old_password;
		$this->new_password = $new_password;
	}
	
	public function do_execute() {
		try {
			$dao = Autor_dao_factory::singleton()->get_dao();
			$autor = $dao->find_by_login_autor($this->login);
			
			$util = new Util_autor();
			
			if (!$util->is_password_encrypted($this->old_password, $autor->get_password())) {
					throw new Parameters_exception('Incorrect password');
			}			

			$dao->update_password($this->login, $util->get_encrypted($this->new_password));
					
		}
		catch (Parameters_exception $e) {
			throw $e;
		}
		catch (Instance_not_found_exception $e) {
			throw new Model_exception($e);
		}
		catch (Internal_error_exception $e) {
			throw $e;
		}
		catch (Exception $e) {
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
}
?>
